home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 676-700 / 681 / term / source.lha / ParseCode.c < prev    next >
C/C++ Source or Header  |  1992-05-09  |  9KB  |  389 lines

  1. /*
  2. **    $Id: ParseCode.c,v 1.4 92/04/21 16:59:12 olsen Sta Locker: olsen $
  3. **    $Revision: 1.4 $
  4. **    $Date: 92/04/21 16:59:12 $
  5. **
  6. **    ANSI control code parsing routines
  7. **
  8. **    Copyright © 1990-1992 by Olaf `Olsen' Barthel & MXM
  9. **        All Rights Reserved
  10. */
  11.  
  12. #include "termGlobal.h"
  13.  
  14.     /* How many characters we will keep in the scan buffer. */
  15.  
  16. #define MAX_SCAN_SIZE    40
  17.  
  18.     /* A couple of internally referenced variables. */
  19.  
  20. STATIC BYTE    CharsInBuffer    = 0,
  21.         ScanStep    = 0;
  22. STATIC UBYTE    SaveBuffer[MAX_SCAN_SIZE * 2];
  23.  
  24.     /* Character access tables. */
  25.  
  26. STATIC UBYTE Table1[256] =
  27. {
  28.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  29.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  30.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  31.     1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,
  32.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  33.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  34.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  35.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  36.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  37.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  38.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  39.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  40.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  41.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  42.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  43.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  44. };
  45.  
  46. STATIC UBYTE Table2[256] =
  47. {
  48.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  49.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  50.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  51.     1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,
  52.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  53.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  54.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  55.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  56.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  57.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  58.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  59.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  60.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  61.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  62.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  63.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  64. };
  65.  
  66. STATIC UBYTE Table3[256] =
  67. {
  68.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  69.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  70.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  71.     1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,
  72.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  73.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  74.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  75.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  76.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  77.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  78.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  79.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  80.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  81.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  82.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  83.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  84. };
  85.  
  86. STATIC UBYTE Table4[256] =
  87. {
  88.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  89.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  90.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  91.     1,1,1,1,1,1,1,1,1,1,0,0,0,1,0,0,
  92.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  93.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  94.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  95.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  96.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  97.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  98.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  99.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  100.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  101.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  102.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  103.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  104. };
  105.  
  106.     /* This structure describes an ANSI control sequence. */
  107.  
  108. struct ControlCode
  109. {
  110.     UBYTE     FirstChar;
  111.     UBYTE    *Match;
  112.     UBYTE     LastChar;
  113.  
  114.     BYTE     ExactSize;
  115.     UBYTE *    (* __regargs Func)(UBYTE *Buffer);
  116. };
  117.  
  118. #define NUM_CODES (sizeof(ANSICode) / sizeof(struct ControlCode))
  119.  
  120.     /* This follows the control code information. */
  121.  
  122. struct ControlCode ANSICode[] =
  123. {
  124.     /* Single Character Sequences. */
  125.  
  126.     'D',    NULL,     0 ,    1,    (APTR)CursorScrollDown,
  127.     'M',    NULL,     0 ,    1,    (APTR)CursorScrollUp,
  128.     'E',    NULL,     0 ,    1,    (APTR)NextLine,
  129.     '7',    NULL,     0 ,    1,    (APTR)SaveCursor,
  130.     '8',    NULL,     0 ,    1,    (APTR)LoadCursor,
  131.     '=',    NULL,     0 ,    1,    (APTR)NumericAppMode,
  132.     '>',    NULL,     0 ,    1,    (APTR)NumericAppMode,
  133.     'N',    NULL,     0 ,    1,    (APTR)Ignore,
  134.     'O',    NULL,     0 ,    1,    (APTR)Ignore,
  135.     'H',    NULL,     0 ,    1,    (APTR)SetTab,
  136.     'Z',    NULL,     0 ,    1,    (APTR)RequestTerminal,
  137.     'c',    NULL,     0 ,    1,    (APTR)Reset,
  138.     '<',    NULL,     0 ,    1,    (APTR)Ignore,
  139.     '~',    NULL,     0 ,    1,    (APTR)Ignore,
  140.     'n',    NULL,     0 ,    1,    (APTR)Ignore,
  141.     '}',    NULL,     0 ,    1,    (APTR)Ignore,
  142.     'o',    NULL,     0 ,    1,    (APTR)Ignore,
  143.     '|',    NULL,     0 ,    1,    (APTR)Ignore,
  144.  
  145.     /* Double Character Sequences. */
  146.  
  147.     '[',    NULL,    's',    2,    (APTR)SaveCursor,
  148.     '[',    NULL,    'u',    2,    (APTR)LoadCursor,
  149.     '(',    NULL,    'A',    2,    (APTR)FontStuff,
  150.     '(',    NULL,    'B',    2,    (APTR)FontStuff,
  151.     '(',    NULL,    '0',    2,    (APTR)FontStuff,
  152.     ')',    NULL,    'A',    2,    (APTR)FontStuff,
  153.     ')',    NULL,    'B',    2,    (APTR)FontStuff,
  154.     ')',    NULL,    '0',    2,    (APTR)FontStuff,
  155.     '#',    NULL,    '3',    2,    (APTR)ScaleFont,
  156.     '#',    NULL,    '4',    2,    (APTR)ScaleFont,
  157.     '#',    NULL,    '5',    2,    (APTR)ScaleFont,
  158.     '#',    NULL,    '6',    2,    (APTR)ScaleFont,
  159.     '#',    NULL,    '8',    2,    (APTR)AlignmentTest,
  160.     ' ',    NULL,    'F',    2,    (APTR)Ignore,
  161.     ' ',    NULL,    'G',    2,    (APTR)Ignore,
  162.  
  163.     /* Multiple Character Sequence. */
  164.  
  165.     '[',    Table3,    'i',    0,    (APTR)PrinterController,
  166.  
  167.     '[',    Table3,    'n',    0,    (APTR)RequestInformation,
  168.     '[',    Table3,    'c',    0,    (APTR)RequestTerminal,
  169.     '[',    Table3,    'h',    0,    (APTR)SetSomething,
  170.     '[',    Table3,    'l',    0,    (APTR)SetSomething,
  171.  
  172.     '[',    Table4,    'h',    0,    (APTR)Ignore,
  173.  
  174.     '[',    Table1,    'A',    0,    (APTR)MoveCursor,
  175.     '[',    Table1,    'B',    0,    (APTR)MoveCursor,
  176.     '[',    Table1,    'C',    0,    (APTR)MoveCursor,
  177.     '[',    Table1,    'D',    0,    (APTR)MoveCursor,
  178.     '[',    Table1,    'K',    0,    (APTR)EraseLine,
  179.     '[',    Table1,    'J',    0,    (APTR)EraseScreen,
  180.     '[',    Table1,    'P',    0,    (APTR)EraseCharacters,
  181.     '[',    Table1,    'L',    0,    (APTR)InsertLine,
  182.     '[',    Table1,    'M',    0,    (APTR)ClearLine,
  183.     '[',    Table1,    'g',    0,    (APTR)SetTabs,
  184.     '[',    Table1,    'q',    0,    (APTR)Ignore,
  185.  
  186.     '[',    Table2,    'H',    0,    (APTR)SetAbsolutePosition,
  187.     '[',    Table2,    'f',    0,    (APTR)SetAbsolutePosition,
  188.     '[',    Table2,    'm',    0,    (APTR)SetAttributes,
  189.     '[',    Table2,    'y',    0,    (APTR)Ignore,
  190.     '[',    Table2,    'r',    0,    (APTR)SetRegion,
  191.  
  192.     '[',    Table1,    'S',    0,    (APTR)ScrollUp,
  193.     '[',    Table1,    'T',    0,    (APTR)ScrollDown
  194. };
  195.  
  196.     /* DoCancel():
  197.      *
  198.      *    Cancel any currently scanned sequence.
  199.      */
  200.  
  201. VOID
  202. DoCancel()
  203. {
  204.     InSequence    = FALSE;
  205.     CharsInBuffer    = ScanStep = 0;
  206. }
  207.  
  208.     /* CSIFake():
  209.      *
  210.      *    This routine was added to support 8-bit control
  211.      *    sequences introduced by a CSI character.
  212.      */
  213.  
  214. VOID
  215. CSIFake()
  216. {
  217.         /* Reset scanner */
  218.  
  219.     DoCancel();
  220.  
  221.         /* Perform as if ESC [ had been transmitted. */
  222.  
  223.     InSequence = ParseCode('[');
  224. }
  225.  
  226.     /* ParseCode(UBYTE c):
  227.      *
  228.      *    Input:    A character to be passed through the ANSI code
  229.      *        parser.
  230.      *
  231.      *    Output:    FALSE if input characters did form a valid ANSI
  232.      *        control sequence or if input characters did not
  233.      *        form an ANSI control sequence at all.
  234.      *
  235.      *        TRUE if input characters did possibly introduce
  236.      *        a valid ANSI control sequence.
  237.      */
  238.  
  239. BYTE __regargs
  240. ParseCode(UBYTE c)
  241. {
  242.     WORD i;
  243.  
  244.         /* ScanStep = 0:    This is the first character
  245.          *            to introduce a control sequence.
  246.          */
  247.  
  248.     if(!ScanStep)
  249.     {
  250.             /* Scan all available codes and try to find
  251.              * a match.
  252.              */
  253.  
  254.         for(i = 0 ; i < NUM_CODES ; i++)
  255.         {
  256.                 /* This character may introduce a
  257.                  * control sequence.
  258.                  */
  259.  
  260.             if(ANSICode[i] . FirstChar == c)
  261.             {
  262.                     /* If this is a single
  263.                      * character control sequence
  264.                      * call the approriate function
  265.                      * and exit immediately.
  266.                      */
  267.  
  268.                 if(ANSICode[i] . ExactSize == 1)
  269.                 {
  270.                     if(Config . Emulation != EMULATION_ATOMIC || Escape)
  271.                     {
  272.                         UBYTE *SomeString;
  273.  
  274.                         SaveBuffer[CharsInBuffer++] = c;
  275.                         SaveBuffer[CharsInBuffer  ] = 0;
  276.  
  277.                         if(SomeString = (*ANSICode[i] . Func)(SaveBuffer))
  278.                             SerWrite(SomeString,strlen(SomeString));
  279.                     }
  280.  
  281.                     CharsInBuffer = ScanStep = 0;
  282.  
  283.                     return(FALSE);
  284.                 }
  285.  
  286.                     /* The length of this control
  287.                      * sequence is greater than
  288.                      * a single character. Save
  289.                      * the input character and
  290.                      * return.
  291.                      */
  292.  
  293.                 ScanStep = i;
  294.  
  295.                 SaveBuffer[CharsInBuffer++] = c;
  296.  
  297.                 return(TRUE);
  298.             }
  299.         }
  300.  
  301.             /* No control sequence introducing character
  302.              * was found.
  303.              */
  304.  
  305.         CharsInBuffer = ScanStep = 0;
  306.  
  307.         return(FALSE);
  308.     }
  309.     else
  310.     {
  311.             /* Length of the control sequence
  312.              * overrides the boundary, exit
  313.              * immediately!
  314.              */
  315.  
  316.         if(CharsInBuffer > MAX_SCAN_SIZE)
  317.         {
  318.             /* No Match */
  319.  
  320.             CharsInBuffer = ScanStep = 0;
  321.  
  322.             return(FALSE);
  323.         }
  324.  
  325.             /* Scan the remaining codes for a match. */
  326.  
  327.         for(i = ScanStep ; i < NUM_CODES ; i++)
  328.         {
  329.                 /* This sequence begins with the
  330.                  * same character the parser was
  331.                  * initialized with, so let's take
  332.                  * a look at it.
  333.                  */
  334.  
  335.             if(ANSICode[i] . FirstChar == SaveBuffer[0])
  336.             {
  337.                     /* This character is supposed to
  338.                      * terminate the sequence, so exit.
  339.                      */
  340.  
  341.                 if(ANSICode[i] . LastChar == c)
  342.                 {
  343.                     if(Config . Emulation != EMULATION_ATOMIC || Escape)
  344.                     {
  345.                         UBYTE *SomeString;
  346.  
  347.                         SaveBuffer[CharsInBuffer++] = c;
  348.                         SaveBuffer[CharsInBuffer  ] = 0;
  349.  
  350.                         if(SomeString = (*ANSICode[i] . Func)(SaveBuffer))
  351.                             SerWrite(SomeString,strlen(SomeString));
  352.                     }
  353.  
  354.                     CharsInBuffer = ScanStep = 0;
  355.  
  356.                     return(FALSE);
  357.                 }
  358.                 else
  359.                 {
  360.                     if(ANSICode[i] . Match)
  361.                     {
  362.                             /* This character is part of
  363.                              * a legal sequence. Store it
  364.                              * and return.
  365.                              */
  366.  
  367.                         if(ANSICode[i] . Match[c])
  368.                         {
  369.                             ScanStep = i;
  370.  
  371.                             SaveBuffer[CharsInBuffer++] = c;
  372.  
  373.                             return(TRUE);
  374.                         }
  375.                     }
  376.                 }
  377.             }
  378.         }
  379.  
  380.             /* This character is not part of a valid
  381.              * ANSI control sequence, so exit.
  382.              */
  383.  
  384.         CharsInBuffer = ScanStep = 0;
  385.  
  386.         return(FALSE);
  387.     }
  388. }
  389.